package dbopr

import (
	"myCloud/dbmodel"
)

type AlbumProvider struct{}

func NewAlbumProvider() *AlbumProvider {
	return &AlbumProvider{}
}

func (*AlbumProvider) GetByUser(userID int) ([]dbmodel.Album, error) {
	var out []dbmodel.Album
	err := dbSession.Where("user_id=?", userID).Find(&out).Error
	return out, err
}

func (*AlbumProvider) FindOrCreate(userId, devID int, name string) (*dbmodel.Album, error) {
	var out dbmodel.Album

	err := dbSession.Where(dbmodel.Album{
		UserID: userId,
		DevID:  devID,
		Name:   name,
	}).Attrs(dbmodel.Album{Name: name}).FirstOrCreate(&out).Error

	return &out, err
}

func (*AlbumProvider) GetById(albumID int) (*dbmodel.Album, bool, error) {
	var out dbmodel.Album
	query := dbSession.Where("id=?", albumID).First(&out)
	if query.RecordNotFound() {
		return nil, true, nil
	}
	return &out, false, query.Error
}

func (p *AlbumProvider) UpdateThumb(albumID int, hash string) error {
	var r = &dbmodel.Album{}
	r.ID = uint(albumID)

	return dbSession.Model(r).Update("thumb", hash).Error
}
